package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Logistics;
import com.fasteat.mall.entity.Orderform;
import com.fasteat.mall.entity.ProductComment;
import com.fasteat.mall.mapper.ProductCommentMapper;
import com.fasteat.mall.service.LogisticsService;
import com.fasteat.mall.service.OrderformService;
import com.fasteat.mall.service.ProductCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-01-19
 */
@RestController
@CrossOrigin
@Api(description = "商品评论接口")
@RequestMapping("/mall/productComment")
public class ProductCommentController {
    @Autowired
    private ProductCommentService productCommentService;

    @Autowired
    private OrderformService orderformService;

    @Autowired(required = false)
    private ProductCommentMapper productCommentMapper;

    /**
     * 新增评论
     * @param productComment
     * @return
     */
    @ApiOperation(value = "新增评论")
    @PostMapping
    public Result save(@RequestBody ProductComment productComment, String orderformId) {
        if(productComment == null) {
            return Result.error("请勿添加空评论");
        }
        if(StringUtils.isEmpty(orderformId)) {
            return Result.error("请添加订单id");
        }
        Orderform orderform = orderformService.getById(orderformId);
        if(orderform == null) {
            return Result.error("查无此订单");
        }
        orderform.setOrderStatus(5);
        boolean ret = orderformService.updateById(orderform);
        boolean result = productCommentService.save(productComment);
        return result && ret ?
                Result.success("添加成功")
                :
                Result.error("添加失败");
    }

    /**
     * 根据评论id删除评论
     * @param productCommentId
     * @return
     */
    @DeleteMapping(value = "/delete/{productCommentId}")
    @ApiOperation(value = "根据评论id删除评论")
    public Result deleteById(@PathVariable(name = "productCommentId", required = true)
                             @ApiParam(value = "productCommentId", required = true)
                                     String productCommentId) {
        if (productCommentId == null) {
            return Result.error("删除失败");
        }
        boolean result = productCommentService.removeById(productCommentId);
        return result ?
                Result.success("删除成功")
                :
                Result.error("删除失败");
    }

    /**
     * 修改评论信息
     * @param productComment
     * @return
     */
    @ApiOperation(value = "根据评论id修改评论信息")
    @PutMapping
    public Result updateProduct(@RequestBody
                                @ApiParam(value = "评论Logistics对象", required = true)
                                            ProductComment productComment) {
        if (StringUtils.isEmpty(productComment.getId())) {
            return Result.error("修改失败");
        }
        boolean result = productCommentService.updateById(productComment);
        return result
                ?
                Result.success("修改成功")
                :
                Result.error("修改失败");
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/{size}/{current}")
    @ApiOperation("分页查看订单")
    public Result acceptProduct(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<ProductComment> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
        productCommentService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 通过评论ID获取评论信息
     *
     * @param productCommentId
     * @return
     */
    @GetMapping("findById/{productCommentId}")
    @ApiOperation("通过评论ID获取评论信息")
    public Result findById(@PathVariable(name = "productCommentId", required = true)
                           @ApiParam(value = "评论ID", required = true)
                                   String productCommentId) {
        if (StringUtils.isEmpty(productCommentId)) {
            return Result.error("查询失败");
        }
        // 从数据库中查询商品的信息
        ProductComment productComment = productCommentService.getById(productCommentId);
        return Result.success("查询成功").data(productComment);
    }

    //根据商品id获取评论
    @GetMapping("findById/{productId}/{size}/{current}")
    @ApiOperation("通过商品ID获取评论信息")
    public Result findByProductId(@PathVariable(name = "productId", required = true) @ApiParam(value = "商品ID", required = true) String productId,
                                  @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                  @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
        if (StringUtils.isEmpty(productId)) {
            return Result.error("查询失败");
        }
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<ProductComment> page = new Page(current, size);
        // 从数据库中查询商品的信息
        QueryWrapper<ProductComment> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        productCommentService.page(page, wrapper);
        return Result.success("查询成功").data(page);
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/ByUserId/{userId}/{limit}/{size}")
    @ApiOperation("分页查看用户评价")
    public Result acceptCommentPageByUserId(@PathVariable(name = "userId") @ApiParam(required = true, value = "用户名") String userId,
                                            @PathVariable(name = "limit") @ApiParam(required = true, value = "开始") Integer limit,
                                            @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") Integer size) {
        return Result.success().data(productCommentMapper.selectBySql(userId, limit, size));
    }
}

